<template>
  <yfu-dialog ref="yfuheaderModifyPasswordDialog" :dialogParam="dialogParam" v-on:dialogOpenBefore="dialogOpenBefore" v-on:dialogCloseBefore="dialogCloseBefore">
    <div>
      <el-form  :model="ruleForm2" label-position="left" size="mini"  :rules="rules2" ref="ruleForm2" label-width="108px" class="demo-ruleForm" style="margin-top: 20px">
        <el-form-item  label="原密码：" prop="oldpass">
          <el-input type="password" v-model="ruleForm2.oldpass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="输入新密码：" prop="pass">
          <el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="确认密码：" prop="checkPass">
          <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </yfu-dialog>
</template>
<script>
  import { updatePassword } from '@/view/api/userManager.js'
  import { yfuSuccessToast } from '@/utils/yfuMessage.js'
  import { encrypt, encryptParameter } from '@/utils/des.js'

  export default {
    data () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        dialogParam: {
          title: '修改密码',
          width: '30%',
          showFooter: true,
          isModal: false
        },
        ruleForm2: {
          pass: '',
          checkPass: '',
          oldpass: ''
        },
        rules2: {
          oldpass: [
            { required: true, trigger: 'blur', message: '原密码不能为空' }
          ],
          pass: [
            { required: true, validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { required: true, validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      openDialog () {
        this.$refs.yfuheaderModifyPasswordDialog.openDialog()
        this.ruleForm2 = {
          pass: '',
          checkPass: '',
          oldpass: ''
        }
      },
      // DIALOG 打开时执行方法
      dialogOpenBefore () {
        // this.$refs.headerModifyPasswordForm.editForm()
      },
      closeDialog () {
        this.$refs.yfuheaderModifyPasswordDialog.closeDialog()
      },
      // DIALOG 点击保存时 执行方法/同时关闭DIALOG
      dialogCloseBefore () {
        let cThis = this

        console.log('close')
        this.$refs.ruleForm2.validate((valid) => {
          if (valid) {
            // 重置密码
            let params = {}
            params.modifyPwdType = 1
            params.passWord = encrypt(encryptParameter.key, encryptParameter.iv, cThis.ruleForm2.oldpass)
            params.id = this.$store.getters.id
            params.newPassword = encrypt(encryptParameter.key, encryptParameter.iv, cThis.ruleForm2.pass)
            updatePassword(params,
              function (data) {
                console.log(data)
                console.log('重置密码成功!')
                cThis.closeDialog()
                yfuSuccessToast('重置密码成功！')
              },
              function (error) {
                console.log(error)
              }
            )
          } else {
            return false
          }
        })
      }
    }
  }
</script>
<style>

</style>
